package cn.qzzg.service.impl;

import cn.qzzg.common.exceptlion.CommonException;
import cn.qzzg.enums.CodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.qzzg.pojo.SamplesPersonnel;
import cn.qzzg.service.SamplesPersonnelService;
import cn.qzzg.mapper.SamplesPersonnelMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 九九
 * @description 针对表【samples_personnel(样本人员表)】的数据库操作Service实现
 * @createDate 2023-03-25 21:51:39
 */
@Service
public class SamplesPersonnelServiceImpl extends ServiceImpl<SamplesPersonnelMapper, SamplesPersonnel>
        implements SamplesPersonnelService {
    /**
     * 通用条件构造器
     */
    private LambdaQueryWrapper<SamplesPersonnel> wrapper;

    /**
     * 通过ID查询全部数据
     *
     * @param objId 需要查询的对象ID
     * @return 结果对象
     */
    @Override
    public List<SamplesPersonnel> findAllByTestTubeId(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SamplesPersonnel::getTestTubeId, objId)
                .orderByDesc(SamplesPersonnel::getCreateTime);
        return list(wrapper);
    }

    /**
     * 添加一条数据
     *
     * @param obj 需要添加的对象
     * @return 添加的对象ID
     */
    @Override
    public Integer saveOne(SamplesPersonnel obj) {
        if (ObjectUtils.isEmpty(obj))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(obj.getIdCard()), SamplesPersonnel::getIdCard, obj.getTestTubeId());
        if (count(wrapper) > 0)
            throw new CommonException(CodeEnum.CUSTOM_ERROR_F0103);
        save(obj);
        return obj.getSamplesPersonnelId();
    }

    /**
     * 通过ID删除一条数据
     *
     * @param objId 需要删除的对象ID
     * @return 删除是否成功
     */
    @Override
    public boolean removeOneByIdCard(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SamplesPersonnel::getIdCard, objId);
        remove(wrapper);
        return true;
    }

    /**
     * 根据ID批量删除数据
     *
     * @param idList 需要删除的对象ID集合
     */
    @Override
    public void batchRemove(List<Integer> idList) {
        if (ObjectUtils.isEmpty(idList) || idList.size() == 0)
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        removeBatchByIds(idList);
    }
}


